package com.mutual.help.controller;

import com.mutual.help.common.JsonResult;
import com.mutual.help.common.PageResult;
import com.mutual.help.entity.Users;
import com.mutual.help.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/toPage")
    public String toPage(){
        return "user/user-list";
    }

    @RequestMapping("/user/toAddPage")
    public String toAddPage(){
        return "user/user-add";
    }

    @RequestMapping("/user/toEditPage")
    public String toEditPage(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        Users users = userService.getById(Integer.parseInt(id));
        model.addAttribute("user",users);
        return "user/user-edit";
    }

    @RequestMapping("/index")
    public String toIndexPage(Model model, HttpSession session){
        Users user = (Users) session.getAttribute("user");
        model.addAttribute("user",user);
        return "index";
    }

    /**
     * 欢迎界面
     * @return
     */
    @RequestMapping("/user/welcome")
    public String welcomePage(){
        return "welcome";
    }

    @RequestMapping("/user/loginOut")
    public String loginOut(HttpSession session, Model mode){
        //在这里执行退出系统前需要清空的数据
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.logout();
            session.removeAttribute("user");
        }catch (Exception e){

        }
        return "login";
    }


    /**
     * 查询所有用户
     * @param page 起始页数
     * @param limit 限制每页多少条数据
     * @return
     */
    @RequestMapping("/user/query")
    @ResponseBody
    public PageResult<Users> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Users> allArticle = userService.getAllUser(page, limit);
        Integer count = userService.countData();
        return new PageResult<>(allArticle, count);
    }

    @RequestMapping("/user/update")
    @ResponseBody
    public PageResult update(Users users){
        if(userService.updateById(users)){
            return new PageResult("修改成功",200);
        }
        return new PageResult("修改失败",400);
    }

    @RequestMapping("/user/add")
    @ResponseBody
    public PageResult add(Users users){
        if(userService.save(users)){
            return new PageResult("新增成功",200);
        }
        return new PageResult("新增失败",400);
    }

    @RequestMapping("/user/delete/{id}")
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(userService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    @PutMapping("/user/psw")
    @ResponseBody
    public JsonResult updatePsw(String oldPsw, String newPsw, String uid) {
        if (!oldPsw.equals(userService.getByUid(uid).getPassword())) {
            return JsonResult.error("原密码不正确");
        }
        if (userService.updatePassword(uid,newPsw)>0) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
}
